avimittal30 commited on
Commit
1ec3c01
·
1 Parent(s): b3932f4

pushing files

Browse files
Files changed (4) hide show
  1. README.md +41 -8
  2. app.py +60 -0
  3. ollama_t.py +46 -0
  4. requirements.txt +6 -0
README.md CHANGED
@@ -1,14 +1,47 @@
1
  ---
2
- title: Ollama Test
3
- emoji: 🏃
4
- colorFrom: green
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.23.3
8
  app_file: app.py
9
  pinned: false
10
- license: apache-2.0
11
- short_description: A small application with llama2 model using Ollama
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Llama 2 Chat
3
+ emoji: 🤖
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: "4.19.2"
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
+ # Llama 2 Chat
13
+
14
+ A simple chat interface for the Llama 2 model using Ollama.
15
+
16
+ ## Features
17
+
18
+ - Chat interface using Gradio
19
+ - Powered by Llama 2 model via Hugging Face
20
+ - Simple and intuitive UI
21
+ - Example prompts included
22
+
23
+ ## Usage
24
+
25
+ 1. Enter your prompt in the text box
26
+ 2. Click "Submit" or press Enter
27
+ 3. Wait for the model's response
28
+ 4. Try the example prompts for quick testing
29
+
30
+ ## Technical Details
31
+
32
+ - Built with Gradio
33
+ - Uses Hugging Face Inference API
34
+ - Deployed on Hugging Face Spaces
35
+
36
+ ## Local Development
37
+
38
+ To run this locally:
39
+
40
+ ```bash
41
+ pip install -r requirements.txt
42
+ python app.py
43
+ ```
44
+
45
+ ## Note
46
+
47
+ This application uses the Hugging Face Inference API to access the Llama 2 model. No local server setup is required.
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+
6
+ # For Hugging Face Spaces deployment:
7
+ # Set these environment variables in your Space settings:
8
+ # OLLAMA_HOST: Your Ollama server URL
9
+ # OLLAMA_MODEL: Model name (e.g., "llama2")
10
+
11
+ # Get environment variables for deployment
12
+ OLLAMA_HOST = os.getenv("OLLAMA_HOST", "http://localhost:11434")
13
+ OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama2")
14
+ API_URL = f"{OLLAMA_HOST}/api/generate"
15
+
16
+ def generate_response(prompt):
17
+ try:
18
+ # Make request to local Ollama server
19
+ response = requests.post(
20
+ API_URL,
21
+ json={
22
+ "model": OLLAMA_MODEL,
23
+ "prompt": prompt,
24
+ "stream": False
25
+ }
26
+ )
27
+
28
+ # Check for specific error cases
29
+ if response.status_code == 404:
30
+ return "Error: Ollama server not found. Make sure Ollama is running and OLLAMA_HOST is set correctly."
31
+ elif response.status_code == 500:
32
+ return "Error: Server error. Check if the model is loaded in Ollama and OLLAMA_MODEL is set correctly."
33
+
34
+ response.raise_for_status()
35
+ return response.json()['response']
36
+ except requests.exceptions.RequestException as e:
37
+ return f"Error: {str(e)}"
38
+ except Exception as e:
39
+ return f"Unexpected error: {str(e)}"
40
+
41
+ # Create Gradio interface
42
+ demo = gr.Interface(
43
+ fn=generate_response,
44
+ inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
45
+ outputs=gr.Textbox(lines=10),
46
+ title="Llama 2 Chat",
47
+ description="Chat with Llama 2 model using local Ollama server. Enter your prompt below and get a response.",
48
+ examples=[
49
+ ["What is artificial intelligence?"],
50
+ ["Tell me a short story about a robot."],
51
+ ["Explain quantum computing in simple terms."]
52
+ ]
53
+ )
54
+
55
+ # Launch the app
56
+ if __name__ == "__main__":
57
+ # For local development
58
+ demo.launch()
59
+ # For Hugging Face Spaces:
60
+ # demo.launch(server_name="0.0.0.0", server_port=7860)
ollama_t.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+
4
+ def generate_response(prompt):
5
+ # Ollama API endpoint
6
+ url = "http://localhost:11434/api/generate"
7
+
8
+ # Request payload
9
+ data = {
10
+ "model": "llama2",
11
+ "prompt": prompt,
12
+ "stream": False
13
+ }
14
+
15
+ try:
16
+ # Send POST request to Ollama server
17
+ response = requests.post(url, json=data)
18
+ response.raise_for_status() # Raise an exception for bad status codes
19
+
20
+ # Parse the response
21
+ result = response.json()
22
+ return result.get('response', 'No response generated')
23
+
24
+ except requests.exceptions.RequestException as e:
25
+ return f"Error connecting to Ollama server: {str(e)}"
26
+
27
+ def main():
28
+ print("Welcome to the Ollama Llama 2 Test Application!")
29
+ print("Type 'quit' to exit")
30
+
31
+ while True:
32
+ # Get user input
33
+ user_input = input("\nEnter your prompt: ")
34
+
35
+ # Check if user wants to quit
36
+ if user_input.lower() == 'quit':
37
+ print("Goodbye!")
38
+ break
39
+
40
+ # Generate and print response
41
+ response = generate_response(user_input)
42
+ print("\nLlama 2 Response:")
43
+ print(response)
44
+
45
+ if __name__ == "__main__":
46
+ main()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit==1.32.0
2
+ huggingface-hub==0.20.3
3
+ requests==2.31.0
4
+ aiohttp==3.9.1
5
+ typing-extensions==4.8.0
6
+ gradio==4.19.2