nikunjcepatel commited on
Commit
77b346f
·
verified ·
1 Parent(s): 6af8cf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -25,7 +25,6 @@ def generate_text(input_text, selected_model, history):
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,34 +45,45 @@ def generate_text(input_text, selected_model, history):
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()
 
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
 
46
  # Handle errors
47
  if response.status_code != 200:
48
+ return f"Error: {response.status_code}, {response.text}", history
49
 
50
+ # Parse and return the content of the response
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 interaction to the history with model name
58
+ history += f"Model: {selected_model}\nUser: {input_text}\nResponse: {generated_response}\n\n"
59
 
60
+ return generated_response, history, history
61
 
62
+ # Define the Gradio layout using Blocks
63
+ with gr.Blocks() as iface:
64
+ # Inject custom CSS using gr.HTML()
65
+ gr.HTML("""
66
+ <style>
67
+ #output-response, #output-history {
68
+ height: 300px;
69
+ overflow: auto;
70
+ border: 1px solid #ddd;
71
+ padding: 10px;
72
+ }
73
+ </style>
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()