Skip to content

Instantly share code, notes, and snippets.

@mberman84
Created November 10, 2023 16:17
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save mberman84/a1291cfb08d0a37c3d439028f3bc5f26 to your computer and use it in GitHub Desktop.
Save mberman84/a1291cfb08d0a37c3d439028f3bc5f26 to your computer and use it in GitHub Desktop.
OpenChat Code
import requests
import json
import gradio as gr
url = "http://localhost:11434/api/generate"
headers = {
'Content-Type': 'application/json',
}
conversation_history = []
def generate_response(prompt):
conversation_history.append(prompt)
full_prompt = "\n".join(conversation_history)
data = {
"model": "mistral",
"stream": False,
"prompt": full_prompt,
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
response_text = response.text
data = json.loads(response_text)
actual_response = data["response"]
conversation_history.append(actual_response)
return actual_response
else:
print("Error:", response.status_code, response.text)
return None
iface = gr.Interface(
fn=generate_response,
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."),
outputs="text"
)
iface.launch()
@bibop
Copy link

bibop commented Nov 11, 2023

(Mac Book Pro M2 - Visua Studio Code)
I followed all your steps and even if I have gradio correctly installed it gives me:

(base) bibop@BibopNewMacBook Ollama % /Users/bibop/.pyenv/versions/3.10.0/bin/python "/Users/bibop/Library/CloudStorage/
GoogleDrive-mrbibop@gmail.com/Il mio Drive/AI_Dev/Ollama/main.py"
Traceback (most recent call last):
File "/Users/bibop/Library/CloudStorage/GoogleDrive-mrbibop@gmail.com/Il mio Drive/AI_Dev/Ollama/main.py", line 38, in
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."),

AttributeError: module 'gradio' has no attribute 'inputs'

Any clue?

@nulltermin8d
Copy link

bibop,

I had this same issue. I am using gradio version 4.2.0. To fix this issue I had to change the code on line 38 from

inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."),
to
inputs="text",

Hope that helps.

@TerrenceMiao
Copy link

Change line 38 to:

inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),

@MarcRene
Copy link

im getting: ValueError: When localhost is not accessible, a shareable link must be created. Please set share=True or check your proxy settings to allow access to localhost.

@BennisonDevadoss
Copy link

Change line 38 to:

inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),

Hey thanks @TerrenceMiao , it is working

@bibop
Copy link

bibop commented Nov 20, 2023 via email

@hiddenkirby
Copy link

hiddenkirby commented Feb 1, 2024

@nulltermin8d, yea that's what I needed! thanks! I am using Python 3.10.8 and Gradio 4.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment