Umang427 commited on
Commit
c557d0d
·
verified ·
1 Parent(s): 9bcde89

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +36 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import g4f
2
+ import gradio as gr
3
+
4
+ # ✅ Function to Get GPT-4 Response
5
+ def get_gpt4_response(user_message, history=[]):
6
+ try:
7
+ # Construct the conversation history for the model
8
+ messages = []
9
+ for user, assistant in history:
10
+ messages.append({"role": "user", "content": user})
11
+ messages.append({"role": "assistant", "content": assistant})
12
+
13
+ # Add the new user message
14
+ messages.append({"role": "user", "content": user_message})
15
+
16
+ # Generate response from GPT-4
17
+ response = g4f.ChatCompletion.create(
18
+ model="gpt-4",
19
+ messages=messages
20
+ )
21
+
22
+ return response # ✅ Return only the response as a string (Gradio handles history)
23
+
24
+ except Exception as e:
25
+ print(f"Error occurred: {e}") # Log the error
26
+ return "⚠️ Sorry, there was an issue processing your request."
27
+
28
+ # ✅ Define the Gradio Interface (Hugging Face requires this)
29
+ demo = gr.ChatInterface(
30
+ fn=get_gpt4_response,
31
+ examples=["Hello!", "Tell me a joke!", "What is AI?"],
32
+ cache_examples=False
33
+ )
34
+
35
+ # ✅ No need for `if __name__ == "__main__"` in Hugging Face
36
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ g4f