Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -25,6 +25,7 @@ def generate_text(input_text, selected_model, history):
|
|
25 |
if history is None:
|
26 |
history = "" # Initialize history if it's None
|
27 |
|
|
|
28 |
response = requests.post(
|
29 |
url="https://openrouter.ai/api/v1/chat/completions",
|
30 |
headers={
|
@@ -45,45 +46,34 @@ def generate_text(input_text, selected_model, history):
|
|
45 |
|
46 |
# Handle errors
|
47 |
if response.status_code != 200:
|
48 |
-
return f"Error: {response.status_code}, {response.text}", history
|
49 |
|
50 |
-
# Parse and
|
51 |
try:
|
52 |
response_json = response.json()
|
53 |
generated_response = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
|
54 |
except json.JSONDecodeError:
|
55 |
generated_response = "Error: Unable to parse response."
|
56 |
|
57 |
-
# Append the new
|
58 |
-
history += f"
|
59 |
|
60 |
-
return generated_response, history, history
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
|
76 |
-
input_text = gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here")
|
77 |
-
selected_model = gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0])
|
78 |
-
|
79 |
-
# Generate button positioned below the dropdown
|
80 |
-
generate_button = gr.Button("Generate")
|
81 |
-
|
82 |
-
output_response = gr.Textbox(label="Response", placeholder="Response will be shown here")
|
83 |
-
output_history = gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False)
|
84 |
-
|
85 |
-
# Trigger the function when the user clicks the "Generate" button
|
86 |
-
generate_button.click(generate_text, inputs=[input_text, selected_model, gr.State()], outputs=[output_response, output_history, gr.State()])
|
87 |
-
|
88 |
-
# Launch the interface
|
89 |
iface.launch()
|
|
|
25 |
if history is None:
|
26 |
history = "" # Initialize history if it's None
|
27 |
|
28 |
+
# Call the API to get the model response
|
29 |
response = requests.post(
|
30 |
url="https://openrouter.ai/api/v1/chat/completions",
|
31 |
headers={
|
|
|
46 |
|
47 |
# Handle errors
|
48 |
if response.status_code != 200:
|
49 |
+
return f"Error: {response.status_code}, {response.text}", history, history
|
50 |
|
51 |
+
# Parse and generate the response
|
52 |
try:
|
53 |
response_json = response.json()
|
54 |
generated_response = response_json.get("choices", [{}])[0].get("message", {}).get("content", "No content returned.")
|
55 |
except json.JSONDecodeError:
|
56 |
generated_response = "Error: Unable to parse response."
|
57 |
|
58 |
+
# Append the new conversation to the history
|
59 |
+
history += f"User: {input_text}\nModel: {selected_model}\nResponse: {generated_response}\n\n"
|
60 |
|
61 |
+
return generated_response, history, history # Return three values
|
62 |
|
63 |
+
# Create Gradio interface with a dropdown for model selection
|
64 |
+
iface = gr.Interface(
|
65 |
+
fn=generate_text,
|
66 |
+
inputs=[
|
67 |
+
gr.Textbox(lines=2, label="Input Text", placeholder="Enter your query here"),
|
68 |
+
gr.Dropdown(choices=MODEL_OPTIONS, label="Select Model", value=MODEL_OPTIONS[0]),
|
69 |
+
gr.State() # This is where we maintain the history state (input)
|
70 |
+
],
|
71 |
+
outputs=[
|
72 |
+
gr.Textbox(label="Response", placeholder="Response will be shown here"),
|
73 |
+
gr.Textbox(label="History", placeholder="Interaction history will be shown here", lines=10, interactive=False),
|
74 |
+
gr.State() # History needs to be output as well
|
75 |
+
],
|
76 |
+
title="Chat with OpenRouter Models"
|
77 |
+
)
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
iface.launch()
|