-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.0.py
49 lines (43 loc) · 1.63 KB
/
main2.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import requests
import json
import subprocess
def fetch_response_from_api(prompt):
api_url = "http://localhost:11434/api/generate" # Replace with the actual API URL
headers = {
"Authorization": "ollama", # Replace with your actual API Key
"Content-Type": "application/json",
}
payload = {
"question": prompt,
}
try:
response = requests.post(api_url, json={"model": "tinyllama", "stream": False, "prompt": prompt}, stream=True)
response.raise_for_status() # Raises an HTTPError if the response status code is 4XX or 5XX
except requests.exceptions.HTTPError as http_err:
return f"HTTP Error occurred: {http_err.response.text}"
except Exception as err:
return f"An error occurred: {err}"
try:
response_lines = response.text.splitlines()
complete_response = ""
for line in response_lines:
json_response = json.loads(line)
complete_response += json_response.get('response', '')
if json_response.get('done', False):
break
return complete_response
except json.JSONDecodeError:
return "Error processing one of the response lines."
def text_to_speech(text):
# Using subprocess to call espeak for text-to-speech functionality
subprocess.call(['espeak', text])
def main():
while True:
prompt = input("Ask a question (or type 'exit' to quit): ")
if prompt.lower() == 'exit':
break
response = fetch_response_from_api(prompt)
print("Response:", response)
text_to_speech(response)
if __name__ == "__main__":
main()